layui.use(['form','layer','jquery','treeSelect'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;



    var treeSelect= layui.treeSelect;//树形下拉框
    treeSelect.render({
        elem: '#tree-select',
        data: '/role/getAll',
        type: 'get',
        placeholder: '权限角色申请',// 占位符
        search: true,// 是否开启搜索功能：true/  false，默认false
        click: function(data){
            $('.layui-role-name').css({'z-index':0});
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            // $("#tree-select").val("0");
        }
    });


    //登录按钮
    form.on("submit(register)",function (data) {

        if(data.field.repass!=data.field.password)
        {
            layer.msg('两次密码不同，注册失败！');
        }
        else {
            $.ajax({
                url: "/verifyUserName",
                type: "POST",
                loginName: data.field.loginName,
                success: function (d) {
                    if (d.code!= 200) {
                        $.ajax({
                            url: "/registerInfor",
                            type: "POST",
                            data:{
                                loginName:data.field.loginName,
                                password:data.field.password,
                                roleId:data.field.roleId
                            },
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.msg('申请注册成功，请等待结果！');
                                    setTimeout(function(){
                                        window.location.href = "/login";
                                    },3000);

                                } else {
                                    layer.msg('注册失败！');

                                }
                            }
                        });

                    } else {
                        layer.msg('该用户名已存在！');

                    }
                }
            })



        }
        return false;
    });






    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
});
